﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAPbouiCOM;
using SAPbobsCOM;

namespace BoAddonBiz_FreightForwarder
{
    [BoAddonBiz_FreightForwarder.BusinessBase.ControlB1Form(IsNew = true, FormType = B1FormTypes.ado_AVA_FF_ArrivePort)]
    public class B1Form_ArrivePort : B1Form_BaseOperationSon
    {
        #region public properties
        SAPBoAddon.B1FormInitializePar oInit;
        Form listForm;
        Form BNOForm;
        public override string FatherField
        {
            get { return "U_IsArrivePort"; }
        }

        public override string FatherUDS
        {
            get { return "OARP_UID"; }
        }

        public override string ThisTableName
        {
            get { return "@AVA_FF_OARP"; }
        }

        public override string ThisObjectCode
        {
            get { return "AVA_FF_ArrivePort"; }
        }

        public override string ThisSubTableName
        {
            get { return null; }
        }
        public override string ThisSubTableName2
        {
            get { return null; }
        }
        #endregion

        #region constructor
        public B1Form_ArrivePort()
            : base()
        {
            this.frm_ChooseFormListEvent += new SAPBoAddon.IB1FormEvents.B1ChooseFormListEventEventHandler(B1Form_ArrivePort_frm_ChooseFormListEvent);
            this.frm_ItemEvent += new SAPBoAddon.IB1FormEvents.B1ItemEventEventHandler(B1Form_ArrivePort_frm_ItemEvent);
            this.frm_FormDataEvent += new SAPBoAddon.IB1FormEvents.B1FormDataEventEventHandler(B1Form_ArrivePort_frm_FormDataEvent);
        }



        #endregion

        #region methods
        void B1Form_ArrivePort_frm_FormDataEvent(SAPBoAddon.B1AddonBase.B1BusinessObjectInfo pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            { }
            else
            {
                if (pVal.EventType == BoEventTypes.et_FORM_DATA_ADD || pVal.EventType == BoEventTypes.et_FORM_DATA_UPDATE)
                {
                    UpdateMainOrderData();
                }
            }
        }
        void B1Form_ArrivePort_frm_ChooseFormListEvent(SAPBoAddon.B1AddonBase.B1ChooseFormList pVal, ref bool BubbleEvent)
        {
            if (pVal.Before_Action)
            {
                SAPbouiCOM.ChooseFromList cfl = this.CurrentForm.ChooseFromLists.Item(pVal.ChooseFromListUID);
                SAPbouiCOM.Conditions cons;
                SAPbouiCOM.Condition con;
                switch (pVal.ChooseFromListUID)
                {
                    case "CFL_OCRD_D"://指定代理
                        break;
                    case "CFL_OCRD_ED"://目的港代理
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "QryGroup7";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_AND;
                        con = cons.Add();
                        con.Alias = "validFor";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                    case "CFL_OCRD_O"://调单单位名称
                        break;
                    case "CFL_OCRD_R"://分单收货人
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "QryGroup11";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_AND;
                        con = cons.Add();
                        con.Alias = "validFor";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                    //case "CFL_OCRD_xD":
                    //case "CFL_OCRD_F":
                    //case "CFL_OCRD_SD":
                    //case "CFL_OCRD_xED":
                    //    cons = new SAPbouiCOM.Conditions();
                    //    con = cons.Add();
                    //    con.Alias = "CardType";
                    //    con.CondVal = "S";
                    //    con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                    //    cfl.SetConditions(cons);
                    //    break;
                }
            }
            else
            {
                if (pVal.Action_Success)
                {
                    SAPbouiCOM.DBDataSource dbds = this.CurrentForm.DataSources.DBDataSources.Item(ThisTableName);
                    if (pVal.ChooseFromListUID == "CFL_OCRD_D")//指定代理
                    {
                        dbds.SetValue("U_DelegateCardCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_DelegateCardName", dbds.Offset, string.IsNullOrEmpty(pVal.SelectedObjects.GetValue("CardFName", 0).ToString()) ? pVal.SelectedObjects.GetValue("CardName", 0).ToString() : pVal.SelectedObjects.GetValue("CardFName", 0).ToString());
                    }
                    else if (pVal.ChooseFromListUID == "CFL_OCRD_ED")//目的港代理
                    {
                        dbds.SetValue("U_EndDelegateCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_EndDelegateName", dbds.Offset, string.IsNullOrEmpty(pVal.SelectedObjects.GetValue("CardName", 0).ToString()) ? pVal.SelectedObjects.GetValue("CardFName", 0).ToString() : pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                    }
                    else if (pVal.ChooseFromListUID == "CFL_OCRD_R")//分单收货人
                    {
                        dbds.SetValue("U_SBLReceiverCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_SBLReceiverName", dbds.Offset, string.IsNullOrEmpty(pVal.SelectedObjects.GetValue("CardFName", 0).ToString()) ? pVal.SelectedObjects.GetValue("CardName", 0).ToString() : pVal.SelectedObjects.GetValue("CardFName", 0).ToString());
                    }
                    else if (pVal.ChooseFromListUID == "CFL_OCRD_O")//调单单位
                    {
                        dbds.SetValue("U_OutOrderCardCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_OutOrderCardName", dbds.Offset, string.IsNullOrEmpty(pVal.SelectedObjects.GetValue("CardName", 0).ToString()) ? pVal.SelectedObjects.GetValue("CardFName", 0).ToString() : pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                        dynamic Cntct = Utils.GetDefaultContractPerson(this.SBOCompany, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        if (Cntct != null)
                        {
                            dbds.SetValue("U_OutOrderCntct", dbds.Offset, Cntct.FullName);
                            dbds.SetValue("U_OutOrderPhone", dbds.Offset, Cntct.Cellolar);
                        }
                    }
                    else if (pVal.ChooseFromListUID == "CFL_PORT")//目的港
                    {
                        dbds.SetValue("U_EndPortCode", dbds.Offset, pVal.SelectedObjects.GetValue("U_PortCode", 0).ToString());
                        dbds.SetValue("U_EndPortEName", dbds.Offset, pVal.SelectedObjects.GetValue("U_ForeignName", 0).ToString());
                        dbds.SetValue("U_Destination", dbds.Offset, pVal.SelectedObjects.GetValue("U_CountryNameE", 0).ToString());
                    }
                    //else if (pVal.ChooseFromListUID == "CFL_OCRD_SD")//起运港代理
                    //{
                    //    dbds.SetValue("U_StartDelegateCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                    //    dbds.SetValue("U_StartDelegateName", dbds.Offset, pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                    //}
                    //else if (pVal.ChooseFromListUID == "CFL_OCRD_xED")//目的港代理
                    //{
                    //    dbds.SetValue("U_xEndDelegateCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                    //    dbds.SetValue("U_xEndDelegateName", dbds.Offset, pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                    //}
                    //else if (pVal.ChooseFromListUID == "CFL_OCRD_xD")//指定代理名称
                    //{
                    //    dbds.SetValue("U_xDelegateCardCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                    //    dbds.SetValue("U_xDelegateCardName", dbds.Offset, pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                    //}
                    //else if (pVal.ChooseFromListUID == "CFL_OCRD_F")//起运港承运人
                    //{
                    //    dbds.SetValue("U_FreighterCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                    //    dbds.SetValue("U_FreighterName", dbds.Offset, pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                    //}

                    //else if (pVal.ChooseFromListUID == "CFL_PORT_S")
                    //{
                    //    dbds.SetValue("U_StartPortCode", dbds.Offset, pVal.SelectedObjects.GetValue("DocEntry", 0).ToString());
                    //    dbds.SetValue("U_StartPortThCode", dbds.Offset, pVal.SelectedObjects.GetValue("U_PortCode", 0).ToString());
                    //    dbds.SetValue("U_StartPort", dbds.Offset, pVal.SelectedObjects.GetValue("U_PortNameE", 0).ToString());
                    //}
                    //else if (pVal.ChooseFromListUID == "CFL_PORT_E")
                    //{
                    //    dbds.SetValue("U_xEndPortCode", dbds.Offset, pVal.SelectedObjects.GetValue("DocEntry", 0).ToString());
                    //    dbds.SetValue("U_xEndPortThCode", dbds.Offset, pVal.SelectedObjects.GetValue("U_PortCode", 0).ToString());
                    //    dbds.SetValue("U_xEndPort", dbds.Offset, pVal.SelectedObjects.GetValue("U_PortNameE", 0).ToString());
                    //}
                }
            }
        }
        void B1Form_ArrivePort_frm_ItemEvent(SAPBoAddon.B1AddonBase.B1ItemEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.Before_Action)
            {

            }
            else
            {
                if (pVal.ItemUID == "1000007" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    Utils.OpenUserDeFinedObjectForm(this.CurrentForm, pVal.ItemUID, "AVA_FF_Port");
                }
                //业务单
                if (pVal.ItemUID == "linkbtn_B" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    Utils.OpenUserDeFinedObjectForm(this.CurrentForm, pVal.ItemUID, "AVA_FF_BusinessOpera");
                }
                if (pVal.ItemUID == "59" && (pVal.EventType == BoEventTypes.et_ITEM_PRESSED || pVal.ItemChanged))
                {
                    if (SAPBoAddon.B1Assistant.Item.GetValue(CurrentForm, "59").ToString().Trim() == "Y")
                    {
                        SetItemVisible(true);
                    }
                    else
                    {
                        SetItemVisible(false);
                    }
                }
                //续程
                if (pVal.ItemUID == "x-GoOn" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    if (CurrentForm.Mode == BoFormMode.fm_OK_MODE)
                    {
                        string transType = this.CurrentForm.DataSources.DBDataSources.Item(ThisTableName).GetValue("U_OnwardTransMode", 0).Trim();
                        if (string.IsNullOrEmpty(transType))
                        {
                            SBOApp.MessageBox("请选择业务类型，再进行操作！");
                            return;
                        }
                        if (!string.IsNullOrEmpty(this.CurrentForm.DataSources.DBDataSources.Item(ThisTableName).GetValue("U_TargetEntry", 0).Trim()))
                        {
                            SBOApp.MessageBox("已生成业务单，不需要再生成！");
                            return;
                        }
                        Recordset rs = this.SBOCompany.GetBusinessObject(BoObjectTypes.BoRecordset);
                        rs.DoQuery(String.Format("select ISNULL(U_Type,N'Air') AS U_Type  from [@AVA_FF_OMOT] where Code='{0}'", transType));
                        if (rs.RecordCount == 1)
                        {
                            string onward = Convert.ToString(rs.Fields.Item("U_Type").Value);
                            switch (onward)
                            {
                                case "Air":
                                    this.ShowForm("AVA_FF_BusinessOpera", this.CurrentForm.UniqueID);
                                    break;
                                case "Sea":
                                    this.ShowForm("AVA_SE_BusinessOpera", this.CurrentForm.UniqueID);
                                    break;
                                case "Land":
                                    this.ShowForm("AVA_LN_BusinessOpera", this.CurrentForm.UniqueID);
                                    break;
                            }
                        }
                    }
                    else
                    {
                        SBOApp.MessageBox("请保存数据后，再进行操作！");
                    }
                }
                /*到货通知日期*/
                if (pVal.ItemUID == "61" && pVal.EventType == BoEventTypes.et_VALIDATE && pVal.ItemChanged)
                {
                    this.NoticeDate(pVal.ItemUID);
                }
                if (pVal.ItemUID == "109" && pVal.EventType == BoEventTypes.et_COMBO_SELECT)
                {
                    SAPBoAddon.B1Assistant.Item.SetValue(BNOForm, "cargo_14", SAPBoAddon.B1Assistant.Item.GetValue(this.CurrentForm, pVal.ItemUID));
                }
                if (pVal.ItemUID == "67" && pVal.ItemChanged)/*包装类型*/
                {
                    BNOForm.DataSources.UserDataSources.Item("PackType").ValueEx = SAPBoAddon.B1Assistant.Item.GetValue(this.CurrentForm, pVal.ItemUID).ToString();
                    Utils.SetPackType(FormType_PackType.OATC_UID, BNOForm);
                    Utils.SetPackType(FormType_PackType.OCTC_UID, BNOForm);
                }
                //if (pVal.ItemUID == "1" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                //{
                //    if (CurrentForm.Mode == BoFormMode.fm_UPDATE_MODE || CurrentForm.Mode == BoFormMode.fm_ADD_MODE)
                //    {
                //        string BaseEntry = SAPBoAddon.B1Assistant.Item.GetValue(CurrentForm, "l_base").ToString();//BaseEntry
                //        Utils.FindForm(BNOForm, BaseEntry);
                //    }
                //}
            }
        }
        /// <summary>
        /// 同步到货通知日期
        /// </summary>
        /// <author>WangPeng</author>
        private void NoticeDate(string ItemUID)
        {
            string NoticeDate = SAPBoAddon.B1Assistant.Item.GetValue(this.CurrentForm, ItemUID).ToString();
            SAPbouiCOM.DBDataSource dbds_BNO = BNOForm.DataSources.DBDataSources.Item("@AVA_FF_OBNO");
            dbds_BNO.SetValue("U_NoticeDate", dbds_BNO.Offset, NoticeDate);
        }

        /// <summary>
        /// 过境
        /// </summary>
        void SetTransit()
        {
            SAPbouiCOM.Form oForm;
            SAPbouiCOM.DBDataSource dbds;
            string IsTransit = string.Empty;
            try
            {
                oForm = CurrentForm;
                dbds = oForm.DataSources.DBDataSources.Item(ThisTableName);

                IsTransit = dbds.GetValue("U_IsOnwardTransit", dbds.Offset).Trim();//SAPBoAddon.B1Assistant.Item.GetValue(oForm, "59").ToString().Trim();
                if (IsTransit == "Y")
                {
                    SetItemVisible(true);
                }
                else
                {
                    SetItemVisible(false);
                    if (!string.IsNullOrEmpty(dbds.GetValue("U_TargetEntry", dbds.Offset).Trim()))
                    {
                        SAPBoAddon.B1Assistant.Item.SetEnabled(oForm, "x-GoOn", false);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 设置过境字段是否可用
        /// </summary>
        /// <param name="oForm"></param>
        /// <param name="Value"></param>
        void SetItemVisible(bool Value)
        {
            SAPbouiCOM.Form oForm;
            try
            {
                oForm = CurrentForm;
                System.Xml.XmlDocument oxml = this.FormSetting.FormXmlDocument;
                foreach (System.Xml.XmlNode oNode in oxml.SelectNodes("//item[contains(@uid,'x-')][@type!='8' or @type!='116']"))
                {
                    string item = oNode.Attributes["uid"].Value;
                    if (oForm.ActiveItem == item)
                    {
                        SAPbouiCOM.EditText oEdit = oForm.Items.Item("35").Specific;
                        oEdit.Active = true;
                    }
                    SAPBoAddon.B1Assistant.Item.SetVisible(oForm, item, Value);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region override methods
        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {
            FormSetting.FormType = B1FormTypes.ado_AVA_FF_ArrivePort;
            FormSetting.FormFileName = "AVA_FF_ArrivePortForm.srf";
            base.FormInfoSetting(FormSetting);
            string[] itmNewModeDisable = new string[] { "3" }; //added by WangPeng 2012年9月6日 15:10:25
        }

        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {
            Utils.InitialItemComboBox(this.SBOCompany, this.CurrentForm.Items.Item("109"), "Code", "Name", "@AVA_FF_CATY", "Code", null, null, true);//货物类型
            Utils.InitialItemComboBox(this.SBOCompany, this.CurrentForm.Items.Item("67"), "Code", "Name", "@AVA_FF_OPCT", "Code", null, null, true);//包装方式
            Utils.InitialItemComboBox(this.SBOCompany, this.CurrentForm.Items.Item("x-57"), "Code", "Name", "@AVA_FF_OMOT", "Code", null, null, true);//运输方式
            Utils.InitialItemComboBox(this.SBOCompany, this.CurrentForm.Items.Item("x-57"), "Code", "Name", "@AVA_FF_OMOT", "Code", null, "U_Type='Air' or U_Type IS NULL");//运输方式
            base.InitializeForm(InitPar);
            oInit = InitPar;
            if (!String.IsNullOrEmpty(InitPar.FatherFormUID))
            {
                listForm = SBOApp.Forms.Item(InitPar.FatherFormUID.Split(';')[0]);
                BNOForm = SBOApp.Forms.Item(listForm.DataSources.UserDataSources.Item("ParentUID").Value);

                DBDataSource dbds_obno = BNOForm.DataSources.DBDataSources.Item("@AVA_FF_OBNO");
                DBDataSource dbds_bno1 = BNOForm.DataSources.DBDataSources.Item("@AVA_FF_BNO1");
                DBDataSource dbds_bno4 = BNOForm.DataSources.DBDataSources.Item("@AVA_FF_BNO4");
                DBDataSource main = this.CurrentForm.DataSources.DBDataSources.Item(this.ThisTableName);

                #region 从业务单带入数据
                this.SetDBDSValue(main, dbds_bno4, "U_SBLReceiverCode", "U_ReceiverCode");
                this.SetDBDSValue(main, dbds_bno4, "U_SBLReceiverName", "U_Receiver");
                this.SetDBDSValue(main, dbds_obno, "U_MBLEntry", "U_MBLEntry");
                this.SetDBDSValue(main, dbds_obno, "U_MBLNumber", "U_MBLNumber");
                this.SetDBDSValue(main, dbds_obno, "U_SBLNumber", "U_SBLNumber");
                this.SetDBDSValue(main, dbds_obno, "U_CargoNumber", "U_Quantity");
                this.SetDBDSValue(main, dbds_obno, "U_RoughWeight", "U_GrossWeight");
                this.SetDBDSValue(main, dbds_obno, "U_NetWeight", "U_BillingWeight");
                this.SetDBDSValue(main, dbds_bno1, "U_CargoType", "U_CargoType");
                this.SetDBDSValue(main, dbds_obno, "U_DelegateCardCode", "U_DelegateCardCode");
                this.SetDBDSValue(main, dbds_obno, "U_DelegateCardName", "U_DelegateCardName");
                this.SetDBDSValue(main, dbds_obno, "U_EndDelegateCode", "U_EndDelegateCode");
                this.SetDBDSValue(main, dbds_obno, "U_EndDelegateName", "U_EndDelegateName");
                this.SetDBDSValue(main, dbds_obno, "U_EndPortCode", "U_EndPortCode");
                this.SetDBDSValue(main, dbds_obno, "U_EndPortThCode", "U_EndPortThCode");
                this.SetDBDSValue(main, dbds_obno, "U_EndPort", "U_EndPort");
                this.SetDBDSValue(main, dbds_obno, "U_ETA", "U_ETA");
                this.SetDBDSValue(main, dbds_obno, "U_ATA", "U_ATA");
                this.SetDBDSValue(main, dbds_obno, "U_InStockDate", "U_ATA");
                this.SetDBDSValue(main, dbds_bno1, "U_CargoType", "U_CargoType");/*货物类型*/
                this.SetDBDSValue(main, dbds_obno, "U_ReleaseAfterPay", "U_ReleaseAfterPay");//见款放单
                #endregion
                #region 过境部分
                SetTransit();
                #endregion
                if (!string.IsNullOrEmpty(BNOForm.DataSources.UserDataSources.Item("PackType").ValueEx))
                {
                    main.SetValue("U_PackForm", main.Offset, BNOForm.DataSources.UserDataSources.Item("PackType").ValueEx);/*包装类型*/
                }
            }
        }
        #endregion

        /// <summary>
        /// 设置DBDS值
        /// </summary>
        /// <author>WangPeng</author>
        private void SetDBDSValue(SAPbouiCOM.DBDataSource Dbds_Target, SAPbouiCOM.DBDataSource Dbds_From, string Field_Target, string Field_From)
        {
            Decimal temp;
            if (Decimal.TryParse(Dbds_Target.GetValue(Field_Target, Dbds_Target.Offset), out temp))
                if (temp != 0 && string.IsNullOrWhiteSpace(Dbds_Target.GetValue(Field_Target, Dbds_Target.Offset)) == false)
                    return;
            Dbds_Target.SetValue(Field_Target, Dbds_Target.Offset, Dbds_From.GetValue(Field_From, Dbds_From.Offset).Trim());
        }
        /// <summary>
        /// 更新主单的数据
        /// </summary>
        private void UpdateMainOrderData()
        {
            string BaseEntry = SAPBoAddon.B1Assistant.Item.GetValue(CurrentForm, "l_base").ToString();//BaseEntry
            string ATA = SAPBoAddon.B1Assistant.Item.GetValue(CurrentForm, "55").ToString();//ATA
            if (string.IsNullOrWhiteSpace(ATA))
                ATA = "null";
            else
                ATA = "'" + ATA + "'";
            SAPbobsCOM.Recordset rs = SBOCompany.GetBusinessObject(BoObjectTypes.BoRecordset);
            rs.DoQuery("UPDATE [@AVA_FF_OBNO] SET U_ATA = " + ATA + " Where DocEntry = " + BaseEntry);
        }
    }
}
